package com.hope;

import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.content.pm.PackageManager;
import android.support.v4.content.WakefulBroadcastReceiver;

import java.util.Calendar;

@TargetApi(Build.VERSION_CODES.KITKAT) 
public class AlarmReceiver extends WakefulBroadcastReceiver{

	private AlarmManager alarmMgr;
	private PendingIntent alarmIntent;
	private Calendar calendar;
	
	
	public void onReceive(Context context, Intent intent){
		Intent service = new Intent(context, AlarmService.class);
		startWakefulService(context, service);
	}
	
	public void setAlarm(Context context){
		
		alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.HOUR_OF_DAY, 14);

		alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
		
		ComponentName receiver = new ComponentName(context, AlarmOnBoot.class);
		PackageManager pm = context.getPackageManager();
		
		pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP); 
	}
	
	public void cancelAlarm(Context context) {
        if (alarmMgr!= null) {
            alarmMgr.cancel(alarmIntent);
        }

        ComponentName receiver = new ComponentName(context, AlarmOnBoot.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
	
}
